﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using NVelocityTemplateEngine;
using NVelocityTemplateEngine.Interfaces;

namespace Velocity.Test.NVelocityTemplateEngine
{
    [TestFixture]
    public class NVelocityAssemblyEngineFixture
    {
        private INVelocityEngine engine;

        [SetUp]
        public void SetUp()
        {
            this.engine = NVelocityEngineFactory.CreateNVelocityAssemblyEngine(
                typeof(NVelocityAssemblyEngineFixture).Assembly.GetName().Name, 
                true);
        }

        [Test]
        public void SmokeTest()
        {
            Assert.IsNotNull(this.engine);
        }

        [Test]
        public void NotExistsAssemblyTest()
        {
            var fault_engine = NVelocityEngineFactory.CreateNVelocityAssemblyEngine(
                "not_exists_assembly", 
                true);

            var expected = "Unable to find resource 'not_exists_assembly.simple.template.vm'";

            Assert.AreEqual(expected, fault_engine.Process(new Hashtable(), "simple.template.vm"));
        }

        [Test]
        public void NotExistsResourceInAssemblyTest()
        {
            var fault_engine = NVelocityEngineFactory.CreateNVelocityAssemblyEngine(
                typeof(NVelocityAssemblyEngineFixture).Assembly.FullName,
                true);

            var expected = String.Format(
                "Unable to find resource '{0}.not_exists_template.vm'",
                typeof(NVelocityAssemblyEngineFixture).Assembly.FullName);

            Assert.AreEqual(expected, fault_engine.Process(new Hashtable(), "not_exists_template.vm"));
        }

        [Test]
        public void SimpleTest()
        {
            DateTime now = DateTime.Now;

            IDictionary context = new Hashtable();
            context["name"] = "John";
            context["surname"] = "Smith";
            context["templateType"] = "hibernate_lib_style";
            context["date"] = now;

            var actual = this.engine.Process(context, "AssemblyResources.simple.template.vm");

            var expected = String.Format(
                Common.EXPECTED,
                now);

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TemplateWithClassTest()
        {
            DateTime now = DateTime.Now;

            IDictionary context = new Hashtable();
            context["clazz"] = new Common.Struct("John", "Smith", "hibernate_lib_style", now);

            var actual = this.engine.Process(context, "AssemblyResources.class.template.vm");

            var expected = String.Format(
                Common.EXPECTED,
                now);

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void TemplateWithArrayTest()
        {
            DateTime now = DateTime.Now;

            IDictionary context = new Hashtable();
            context["string_array"] = new object[] { "John", "Smith", "hibernate_lib_style", now };

            var actual = this.engine.Process(context, "AssemblyResources.array.template.vm");

            var expected = String.Format(
                Common.ARRAY_TEMPLATE_EXPECTED,
                now);

            Assert.AreEqual(expected, actual);
        }
    }
}
